<?php

namespace app\command;

use app\adminapi\logic\RepairLogic;
use app\common\model\Huanxin;
use app\common\model\InsurancePolicy;
use app\common\model\Repair;
use app\common\model\user\User;
use think\console\Command;
use think\console\Input;
use think\console\Output;

class GenerateRepair extends Command
{

    protected function configure(){
        $this->setName('g_repair');
    }
    private function generateExpressNumber()
    {
        $expressCompanies = ['SF', 'YT', 'ST', 'ZTO', 'YD']; // 顺丰、圆通、申通、中通、韵达
        $companyCode = $expressCompanies[array_rand($expressCompanies)];
        $randomNumber = str_pad(mt_rand(100000000, 999999999), 9, '0', STR_PAD_LEFT);
        return $companyCode . $randomNumber;
    }
    private function generateRandomAddress()
    {
        $provinces = ['北京市', '上海市', '广东省', '江苏省', '浙江省'];
        $cities = ['广州市', '深圳市', '南京市', '杭州市', '苏州市'];
        $districts = ['天河区', '福田区', '鼓楼区', '西湖区', '工业园区'];

        $province = $provinces[array_rand($provinces)];
        $city = $cities[array_rand($cities)];
        $district = $districts[array_rand($districts)];
        $streetNumber = mt_rand(1, 100) . '号';
        $buildingNumber = '第' . mt_rand(1, 10) . '栋';
        $roomNumber = '第' . mt_rand(101, 999) . '室';

        return $province . $city . $district . $streetNumber . $buildingNumber . $roomNumber;
    }
    protected function execute(Input $input, Output $output)
    {

        for ($i = 0; $i < 8000; $i++) {
            /**
             * @var User $user
             */
            $user = User::orderRand()->findOrFail();


            /**
             * @var InsurancePolicy $insurancePolicy
             */
            $insurancePolicy = $user->insurancePolicyList()->orderRand()->find();
            if ($insurancePolicy){
                $time = date('Y-m-d H:i:s',strtotime($user->create_time)+ mt_rand(0, 30)*86400) ;
                $repair = RepairLogic::addUserRepair($user, $insurancePolicy, [], '维修');
                $repair->order_amount = mt_rand(100, $insurancePolicy->price);
                $repair->create_time = strtotime($time) + mt_rand(0, 2)*86400;
                $repair->pay_time =   strtotime($time) + mt_rand(0, 2)*86400;
                $repair->pay_status = 1;
                $repair->pay_way = 2;
                $repair->express_no = $this->generateExpressNumber();
                $repair->express_type =substr($repair->express_no,0,2);
                $repair->order_no = generate_sn(Repair::class,'order_no','R','',strtotime($time));
                $repair->finish_repari_time = date('Y-m-d H:i:s',strtotime($time) + mt_rand(2,5 )*86400) ;
                $repair->save();
            }
        }






    }
    private function generatePhone()
    {
        $prefixes = [
            '130', '131', '132', '145', '155', '156', '166', '171', '175', '176', '185', '186', '196', // 联通
            '134', '135', '136', '137', '138', '139', '147', '150', '151', '152', '157', '158', '159', '172', '178', '182', '183', '184', '187', '188', '195', '197', '198', // 移动
            '133', '149', '153', '173', '174', '177', '180', '181', '189', '190', '191', '193', '199' // 电信
        ];
        // 随机选择一个号段
        $prefix = $prefixes[array_rand($prefixes)];
        // 随机生成后 8 位数字
        $suffix = str_pad(mt_rand(0, 99999999), 8, '0', STR_PAD_LEFT);
        // 拼接手机号
        return $prefix . $suffix;

    }


    private function generateRandName()
    {
        $surnames = ['赵', '钱', '孙', '李', '周', '吴', '郑', '王', '冯', '陈', '褚', '卫', '蒋', '沈', '韩', '杨'];
        // 定义常见名字数组
        $names = ['伟', '芳', '娜', '敏', '静', '强', '磊', '军', '洋', '勇', '艳', '杰', '娟', '涛', '明', '超'];
        // 随机选择一个姓氏
        $surname = $surnames[array_rand($surnames)];
        // 随机决定名字长度（1 或 2 个字）
        $nameLength = rand(1, 2);
        $name = '';
        if ($nameLength === 1) {
            // 随机选择一个名字
            $name = $names[array_rand($names)];
        } else {
            // 随机选择两个名字
            $name = $names[array_rand($names)] . $names[array_rand($names)];
        }
        return $surname. $name;

    }
}